package app.cn.com.school.activity

import android.app.Dialog
import android.content.ContentValues
import android.content.Context
import android.content.Intent
import android.databinding.DataBindingUtil
import android.graphics.drawable.Drawable
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import android.widget.GridView
import android.widget.ListView
import android.widget.TextView
import app.cn.com.school.R
import app.cn.com.school.adapter.StaTypeAdapter
import app.cn.com.school.adapter.StationeryAdapter
import app.cn.com.school.databinding.ActivityStationeryBinding
import app.cn.com.school.databinding.StaTypeDialogBinding
import com.handmark.pulltorefresh.library.PullToRefreshBase
import org.w3c.dom.Text

class StationeryActivity : BaseActivity<ActivityStationeryBinding>() {
    override fun initBindingVar() {
    }

    override fun initTitle() {
        binding.titlebar.back.setOnClickListener { finish() }
        binding.titlebar.menuDate.visibility = View.VISIBLE
        binding.titlebar.menuDate.setImageResource(R.mipmap.shopping_car)
        binding.titlebar.menuDate.setOnClickListener {
            context.startActivity(Intent(context, ShoppingCarActivity::class.java))
        }
    }
    var flag=true
    lateinit var gridView: GridView
    var adapter: StationeryAdapter? = null
    var adapterType: StaTypeAdapter? = null
    var moreList = ArrayList<ContentValues>()
    lateinit var context: Context
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initBinding(R.layout.activity_stationery)
        context = this
        initView()
    }


    private fun initView() {
        var type = this.intent.getStringExtra("type").toInt()
        when (type) {
            0 -> {
                binding.titlebar.title.text = "精品文具"
            }
            1 -> {
                binding.titlebar.title.text = "学习冲浪"
            }
            2 -> {
                binding.titlebar.title.text = "奇幻文字"
            }
            3 -> {
                binding.titlebar.title.text = "青春杂志"
            }
        }
        moreList.add(ContentValues())
        moreList.add(ContentValues())
        moreList.add(ContentValues())
        moreList.add(ContentValues())
        moreList.add(ContentValues())
        moreList.add(ContentValues())
        moreList.add(ContentValues())
        binding.staMo.setOnClickListener {
            setImg(R.mipmap.xu)
            textColor(binding.staMo, binding.staXu, binding.staType)
        }
        binding.staXu.setOnClickListener {
            if(flag){
                setImg(R.mipmap.x_up)
                flag=false
            }else{
                setImg(R.mipmap.x_down)
                flag=true
            }

            textColor(binding.staXu, binding.staMo, binding.staType)
        }
        binding.staType.setOnClickListener {
            adapterType == null
            dx()
        }




        binding.staGirdView.setOnRefreshListener(onListener2)
        binding.staGirdView.getLoadingLayoutProxy(false, true).setPullLabel("上拉中")
        binding.staGirdView.mode = PullToRefreshBase.Mode.BOTH
        binding.staGirdView.getLoadingLayoutProxy(false, true).setRefreshingLabel("刷新中")
        binding.staGirdView.getLoadingLayoutProxy(false, true).setReleaseLabel("释放刷新")
        gridView = binding.staGirdView.refreshableView

        if (adapter == null) {
            adapter = StationeryAdapter(context, moreList)
            gridView.adapter = adapter
        }
    }

    private fun textColor(v1: TextView, v2: TextView, v3: TextView) {
        v1.setTextColor(resources.getColor(R.color.group_booking))
        v2.setTextColor(resources.getColor(R.color.text))
        v3.setTextColor(resources.getColor(R.color.text))
    }

    private fun setImg(img: Int) {
        var x_up = resources.getDrawable(img)
        x_up.setBounds(0, 0, x_up.minimumWidth, x_up.minimumHeight)
        binding.staXu.setCompoundDrawables(null, null, x_up, null)
    }


    private fun dx() {
        val dialog = Dialog(context, R.style.Theme_Light_Dialog)
        val dialogView = LayoutInflater.from(context).inflate(R.layout.sta_type_dialog, null)
        val window = dialog.window
        window!!.setGravity(Gravity.RIGHT)
        window.setWindowAnimations(R.style.mystyle)
        window.decorView.setPadding(0, 0, 0, 0)
        val lp = window.attributes
        lp.width = WindowManager.LayoutParams.MATCH_PARENT
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT
        window.attributes = lp
        //将自定义布局加载到dialog上
        dialog.setContentView(dialogView)
        val llist = dialogView.findViewById(R.id.type_list) as ListView
        adapterType = StaTypeAdapter(context, moreList)
        llist.adapter = adapterType


        llist.setOnItemClickListener { parent, view, position, id ->
            dialog.hide()
        }
        dialog.show()
    }


    /**
     * 上拉下拉
     * */
    internal var onListener2: PullToRefreshBase.OnRefreshListener2<GridView> = object : PullToRefreshBase.OnRefreshListener2<GridView> {
        override fun onPullUpToRefresh(refreshView: PullToRefreshBase<GridView>?) {
            binding.staGirdView.onRefreshComplete()
        }

        override fun onPullDownToRefresh(refreshView: PullToRefreshBase<GridView>?) {
            binding.staGirdView.onRefreshComplete()
        }
    }

}
